/*
 * Copyright 2009-2010 the original author or authors.
 *
 * Licensed under the Apache license, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.internna.iwebjtracker.server.scm.model;

import java.util.Set;
import java.util.HashSet;
import java.util.Collection;
import java.util.Collections;

import org.internna.iwebmvc.utils.CollectionUtils;

/**
 * A collection of changes that are grouped together in an atomic commit.
 *
 * @author Jose Noheda
 * @since 1.0
 */
public final class ChangeSet<C extends Change> {

    private final Set<C> changes = new HashSet<C>();

    /**
     * Retrieves the collection of changes that happened in this commit.
     *
     * @return a non null unmodifiable collection
     */
    public Collection<C> getChanges() {
        return CollectionUtils.isEmpty(changes) ? Collections.EMPTY_SET : Collections.unmodifiableSet(changes);
    }

    /**
     * Stores a new change in this changeset as long as it does not already exists and it's not null.
     *
     * @param change any
     */
    public void addChange(C change) {
        if (changes != null) {
            changes.add(change);
        }
    }

}
